package com.platform.mobile.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.mobile.bean.LoginAssitantInfo;
import com.platform.mobile.bean.LoginRequest;
import com.platform.mobile.dao.MLoginDao;
import com.platform.mobile.entity.VisitUser;
import com.platform.mobile.service.MLoginService;

@Service
public class MLoginServiceImpl implements MLoginService{

	private static final Logger log = LoggerFactory.getLogger(MLoginServiceImpl.class);
	
	@Autowired
	private MLoginDao dao;
	
	@Override
	public LoginAssitantInfo checkUserInfo(LoginRequest request) {
		
		LoginAssitantInfo bean = new LoginAssitantInfo();
		
		boolean isSuccess = dao.checkUserInfo(request.getUsername());
		if(isSuccess){
			VisitUser vu = dao.getVisitUser(request.getUsername());
			
			if(vu != null){
				bean = entityToBean(vu);
			}
			return bean;
		} else {
			return null;
		}
	}

	@Override
	public List<LoginAssitantInfo> getDoctorArray(String assitant_uid) {

		List<LoginAssitantInfo> doctor_Array = new ArrayList<LoginAssitantInfo>(); 
		
		List<VisitUser> doctorArray 
			= dao.getVisitUserByAssitantId(assitant_uid);
		doctor_Array
			= entityToBeanArray(doctorArray);
		
		return doctor_Array;
	}
	
	private LoginAssitantInfo entityToBean(VisitUser vu){
		LoginAssitantInfo bean = new LoginAssitantInfo();
		bean.setId(vu.getId().toString());
		bean.setAddress(vu.getAddress());
		bean.setAge(vu.getAge());
		bean.setLogin_name(vu.getLogin_name());
		bean.setBirthday(vu.getBirthday());
		bean.setBirthplace(vu.getBirthplace());
		bean.setCert_status(vu.getCert_status());
		bean.setChannel_id(vu.getChannel_id());
		bean.setDept_id(vu.getDept_id());
		bean.setDocDesc(vu.getDocDesc());
		bean.setEducation(vu.getEducation());
		bean.setGoal(vu.getGoal());
		bean.setId_card_no(vu.getId_card_no());
		bean.setLast_update_date(vu.getLast_update_date());
		bean.setMajor(vu.getMajor());
		//bean.setMarriage(vu.getMarriage());
		bean.setNation(vu.getNation());
		bean.setOrg_id(vu.getOrg_id());
		bean.setPhone(vu.getPhone());
		bean.setPost(vu.getPost());
		bean.setQualifiedThumb(vu.getQualifiedThumb());
		bean.setReal_name(vu.getReal_name());
		bean.setRemark(vu.getRemark());
		bean.setSex(vu.getSex());
		bean.setSpeciality(vu.getSpeciality());
		bean.setThumb(vu.getThumb());
		bean.setUser_type(vu.getUser_type());
		bean.setVisit_time(vu.getVisit_time());
		bean.setWork_unit_phone(vu.getWork_unit_phone());
		
		return bean;
	}
	
	private List<LoginAssitantInfo> entityToBeanArray(List<VisitUser> entity){
		
		List<LoginAssitantInfo> doctor_array 
			= new ArrayList<LoginAssitantInfo>();
		
		for(VisitUser vu : entity){
			doctor_array.add(entityToBean(vu));
		}
		return doctor_array;
	}

}
